#include "RustCargoTomlFileVisitor.h"
#include <iostream>

RustCargoTomlFileVisitor::RustCargoTomlFileVisitor(
    const std::string& outputDir,
    const std::string& idlFileName,
    CLangTypeNameVisitor* typeNameVisitor,
    CLangTypeHeaderVisitor* headerVisitor)
    : FileGenVisitor(outputDir, "Cargo.toml")
    , typeNameVisitor_(typeNameVisitor)
    , headerVisitor_(headerVisitor)
    , idlFileName_(idlFileName) {
}

RustCargoTomlFileVisitor::~RustCargoTomlFileVisitor() {
}

void RustCargoTomlFileVisitor::visit(SpecificationNode& node) {
    // 获取包名，通常可用文件名或类型名
    std::string packageName = idlFileName_;
    auto pos = packageName.find_last_of('.');
    if (pos != std::string::npos) {
        packageName = packageName.substr(0, pos);
    }
    generateCargoToml(packageName);
    writeToFile();
}

void RustCargoTomlFileVisitor::generateCargoToml(const std::string& packageName) {
    content_  = "[package]\n";
    content_ += "name = \"" + packageName + "\"\n";
    content_ += "version = \"0.1.0\"\n";
    content_ += "edition = \"2024\"\n";
    content_ += "build = \"build.rs\"\n\n";
    content_ += "[dependencies]\n\n";
    content_ += "[build-dependencies]\n";
    content_ += "cc = \"1.0\"\n";
    content_ += "bindgen = \"*\"\n";
}